package com.gem.netty.message.swap;

import com.gem.entity.Game;
import com.gem.enums.ClientMessageType;
import com.gem.enums.GameRoundStageType;
import com.gem.enums.GemType;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.Content;
import com.gem.netty.entity.CurrentUser;
import com.gem.utils.R;

import java.util.Map;

/**
 * Author: Xhy
 * CreateTime: 2022-12-14 21:56
 */
public class GetGem extends AbstractSwapBase{
    @Override
    protected R isVerify(ClientContent content) {
        Integer gem = content.getContent().getGem();
        // 是否为空 是否宝石类型非法
        if (gem == null || (gem < 0 && gem > 6)){
            return R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("非法操作").type(ClientMessageType.GET_GEM.type);
        }
        return R.ok();
    }

    @Override
    protected R isStock(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());

        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        if (game.getGems().get(user.getCurrentGetGemInfo()) <= 0){
            return R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("宝石库已空").type(ClientMessageType.GET_GEM.type);
        }
        return R.ok();
    }

    @Override
    protected R isOwnNobility(ClientContent content) {
        return R.ok();
    }

    @Override
    protected R isSwap(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        Integer currentGem = user.getCurrentGetGemInfo();
        Map<Integer, Integer> gems = user.getCurrentGemState();
        boolean isNeedBreak = false;
        R r = R.error();
        r.data("color", MessageColorType.SYSTEM_MESSAGE.type);
        r.type(ClientMessageType.GET_GEM.type);
        // 当前玩家已拥有10个宝石
        if (user.getGetGemNumber() == 10){

            return r.message("你已经拥有10个宝石了");
        }else if (gems.get(currentGem) == GemType.GOLD.type && gems.size() != 0){
            // 是否拿的是黄金,回合宝石库中没拿过宝石才可拿黄金
            return r.message("你已拿过宝石,不能拿黄金了");
        }else if (gems.containsKey(currentGem) && gems.size() == 2){
            // 当前只能拿3个不一样的，玩家却拿了2个一样的
            return r.message("你只能拿3个不一样的宝石");
        }else if (gems.containsKey(currentGem) && game.getGems().get(currentGem) <= game.getUsers().size()){
            // 拿两个一样的 宝石库小于等于场上人数则不能拿
            return r.message("相应的宝石数量已经拿不了相同2个宝石了");
        }else if (gems.containsKey(currentGem)){
            gems.put(currentGem,2);
        }else {
            // 拿的是黄金
            if (currentGem == GemType.GOLD.type && gems.size() >= 1){
                return R.error().message("你拿过宝石了,不能拿黄金了");
            }else {
                // 正常拿牌
                gems.put(currentGem,1);
            }

        }
        return R.ok();
    }

    @Override
    protected R isSwapCardOrGetGemRound(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());

        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        if (user.getState() != GameRoundStageType.GET_GEM.type){
            return R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).type(ClientMessageType.GET_GEM.type).message("不是拿宝石回合");
        }
        return R.ok();
    }
}
